package be.kuleuven.peno3.mobiletoledo.view.campusmap;

import java.util.List;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class CampusMap extends MapActivity {
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		if(!isOnline(this.getApplicationContext())) {
			Toast.makeText(this, "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.campusmaplayout);

		MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setSatellite(true);
		mapView.setBuiltInZoomControls(true);

		MapController mc = mapView.getController();
		Bundle coord = this.getIntent().getExtras();
		double lat = coord.getDouble("latitude");
		double lng = coord.getDouble("longitude");

		GeoPoint p = new GeoPoint(
				(int) (lat * 1E6), 
				(int) (lng * 1E6));

		List<Overlay> mapOverlays = mapView.getOverlays();
		MapOverlay itemizedoverlay = new MapOverlay(this.getResources().getDrawable(R.drawable.mapmarker));

		OverlayItem overlayitem = new OverlayItem(p, coord.getString("name"), coord.getString("description"));
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);

		mc.animateTo(p);
		mc.setZoom(17);
		mapView.invalidate();
	}
	
	private static boolean isOnline(Context ctx) {
		ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		try {
			return cm.getActiveNetworkInfo().isConnected();
		} catch(NullPointerException e) {
			return false;
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
